/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.account.controller.member;

import com.rzico.account.entity.PhonePayment;
import com.rzico.account.service.PhonePaymentService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.entity.SysMch;
import com.rzico.core.entity.SysPlugin;
import com.rzico.core.entity.SysUser;
import com.rzico.core.plugin.PaymentPlugin;
import com.rzico.core.service.SysMchService;
import com.rzico.core.service.SysPluginService;
import com.rzico.core.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 销售单控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "豆讯话费订单支付接口")
@RestController
@RequestMapping("/member/phonePayment")
public class PhonePaymentMemberController extends BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysPluginService sysPluginService;

    @Autowired
    private SysMchService sysMchService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private PhonePaymentService phonePaymentService;

    /**
     * 发起支付
     *
     * @param sn
     * @return
     */
    @Log(desc = "发起支付", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("发起支付")
    @PostMapping("/submit")
    public CommResult<PhonePayment> submit(String sn, String paymentPluginId, HttpServletRequest request) {

        PhonePayment phonePayment = phonePaymentService.findBySn(sn);
        if (phonePayment==null) {
            return CommResult.error("无效付款单号");
        }

        Enterprise enterprise = enterpriseService.findById(phonePayment.getEnterpriseId());

        SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
        if (sysMch.getMchType().equals(2)) {
            //特约商户,服务商代收
            enterprise = enterpriseService.findByMch(sysMch.getIsvId());
        }

        SysPlugin sysPlugin = sysPluginService.getEnabledPlugin(enterprise.getMchId(),paymentPluginId);
        if (sysPlugin==null) {
            return CommResult.error("无效插件编号");
        }

        PaymentPlugin paymentPlugin = sysPluginService.getPaymentPlugin(sysPlugin.getPluginId());

        Member member = memberService.selectByPrimaryKey(phonePayment.getMemberId());
        if (member==null) {
            return CommResult.error("无效注册会员");
        }
        SysUser sysUser = sysUserService.findById(member.getUserId());
        if (sysUser==null) {
            return CommResult.error("无效注册用户");
        }

        Map<String,Object> resultMap = paymentPlugin.trade(sysPlugin,sn,phonePayment.getMemo(),phonePayment.getAmount(),paymentPlugin.getUserId(sysUser),request);

        if ("0000".equals(resultMap.get("return_code"))) {
            phonePayment.setModifyDate(new Date());
            phonePayment.setPaymentDate(new Date());
            phonePayment.setPaymentPluginId(paymentPlugin.getId());
            phonePayment.setPaymentPluginName(paymentPlugin.getName());
            phonePayment.setTranSn(String.valueOf(resultMap.get("tranSn")));
            phonePaymentService.updateByPrimaryKeySelective(phonePayment);
            return CommResult.success(resultMap);
        } else {
            return CommResult.error(resultMap.get("error_msg").toString());
        }

    }

}
